global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"

capture log close
log using "$log/6_fig_diff_mode", replace

set scheme plotplain
graph set window fontface "Times New Roman"

*Calculate each worker's change in base pay from Dec 2016 to Dec 2017
	use yr_month client_id state emp_pur_c salaried base if (yr_month==mdy(12,01,2016) | yr_month==mdy(12,01,2017)) using "$output\stayers_panel_treat.dta", clear
	gen time=yr_month==mdy(12,01,2017)
	drop yr_month 
	reshape wide base salaried, i(state client_id emp_pur_c) j(time)
	gen diff_base=round(100*(base1-base0)/base0,0.01)
	drop if base0==.
	gen bunched = base0>=913 & base0<953 & salaried0==1
	
	*Calculate modal pay increase within firm
		egen mode_diff=mode(diff_base), by(client_id)
		egen mode_diff_sal=mode(diff_base) if salaried0==1, by(client_id)
		egen mode_diff_hr=mode(diff_base) if salaried0==0, by(client_id)
		egen employment=count(base0), by(client_id)
		egen employment_sal=count(base0) if salaried0==1, by(client_id)
		sum mode_diff, det
		sum mode_diff if employment>=50, det
		gen missing_mode=mode_diff==.
		sum missing_mode
		sum missing_mode if employment>=50 
		
	*Calculate the mode leaving out bunched workers
		preserve
			drop if bunched==1 
			egen mode_diff_leave_out=mode(diff_base), by(client_id)
			egen mode_diff_sal_leave_out=mode(diff_base) if salaried0==1, by(client_id)
			egen employment_leave_out=count(base0), by(client_id)
			egen employment_sal_leave_out=count(base0) if salaried0==1, by(client_id)
			keep client_id state *_leave_out
			duplicates drop client_id state, force
			tempfile temp
			save `temp'
		restore 
		merge m:1 client_id state using `temp'
			
	*Difference from mode
		gen diff_mode=diff_base-mode_diff
		gen diff_mode_sal=diff_base-mode_diff_sal
		gen diff_mode_leave_out=diff_base-mode_diff_leave_out
		gen diff_mode_sal_leave_out=diff_base-mode_diff_sal_leave_out
		
	*What share of workers receive exactly the modal pay increase?
		gen at_mode=abs(diff_mode)<=0.01
		gen at_mode_sal=abs(diff_mode_sal)<=0.01 & salaried0==1 
		gen at_mode_hr=abs(mode_diff_hr-diff_base)<=0.01 & salaried0==0 
		
		sum at_mode if employment>=50 & mode_diff!=.
		sum at_mode_sal if employment>=50 & salaried0==1 & mode_diff_sal!=. 
		sum at_mode_hr if employment>=50 & salaried0==0 & mode_diff_hr!=.
		
		preserve 
			keep if employment>=50 & mode_diff!=.
			collapse at_mode at_mode_sal at_mode_hr, by(client_id)
			sum at_mode
			replace at_mode=round(at_mode,0.01)*100
			
			histogram at_mode, width(1) discrete percent ///
				xtitle("Share of Workers Receiving Modal Pay Increase") ///
				ytitle("Share of Firms") xlabel(0(20)100)
			graph export "$graphs/figA4b_at_mode.eps", replace
			graph export "$graphs/figA4b_at_mode.jpg", replace
		restore 
		
		preserve 
			keep if employment>=50 & mode_diff!=. & mode_diff_sal!=. & salaried0==1
			collapse at_mode at_mode_sal at_mode_hr, by(client_id)
			sum at_mode_sal
			replace at_mode_sal=round(at_mode_sal,0.01)*100
			
			histogram at_mode_sal, width(1) discrete percent ///
				xtitle("Share of Salaried Workers Receiving Modal Pay Increase") ///
				ytitle("Share of Firms") xlabel(0(20)100) ylabel(0(1)4)
			graph export "$graphs/figA4b_at_mode_sal.eps", replace
			graph export "$graphs/figA4b_at_mode_sal.jpg", replace
		restore 

		preserve 
			keep if employment>=50 & mode_diff!=. & mode_diff_hr!=. & salaried0==0
			collapse at_mode at_mode_sal at_mode_hr, by(client_id)
			sum at_mode_hr
			replace at_mode_hr=round(at_mode_hr,0.01)*100
			
			histogram at_mode_hr, width(1) discrete percent ///
				xtitle("Share of Hourly Workers Receiving Modal Pay Increase") ///
				ytitle("Share of Firms") xlabel(0(20)100)
			graph export "$graphs/figA4b_at_mode_hr.eps", replace
			graph export "$graphs/figA4b_at_mode_hr.jpg", replace
		restore 
		
	*Keep only the bunched workers
		keep if salaried0==1 
		keep if base0>=913 & base0<953
		
	*Average pay increase 
		sum diff_base, det
		
	*Generate bins
		gen bin=round(diff_mode)
		gen bin_sal=round(diff_mode_sal)
		gen bin_leave=round(diff_mode_leave_out)
		gen bin_sal_leave=round(diff_mode_sal_leave_out)
		
	*Plot histogram 
	count if abs(diff_mode)<=50
	histogram bin if abs(diff_mode)<=50, width(1) discrete percent xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-40(20)40) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode.eps", replace
	graph export "$graphs/figA4_diff_mode.jpg", replace
	
	count if abs(diff_mode_sal)<=50
	histogram bin_sal if abs(diff_mode_sal)<=50, width(1) discrete percent xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-40(20)40) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_sal.eps", replace
	graph export "$graphs/figA4_diff_mode_sal.jpg", replace
	
	*Firms with at least 50 employees
	count if abs(diff_mode)<=50 & employment>=50
	histogram bin if abs(diff_mode)<=50 & employment>=50, width(1) discrete percent xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-40(20)40) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_emp50.eps", replace
	graph export "$graphs/figA4_diff_mode_emp50.jpg", replace
	
	count if abs(diff_mode_sal)<=50 & employment_sal>=50
	histogram bin_sal if abs(diff_mode_sal)<=50 & employment_sal>=50, width(1) discrete percent xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-40(20)40) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_sal_emp50.eps", replace
	graph export "$graphs/figA4_diff_mode_sal_emp50.jpg", replace

	*At least 50 employees and mode is not 0
	count if abs(diff_mode)<=50 & employment>=50 & mode_diff!=0
	histogram bin if abs(diff_mode)<=50 & employment>=50 & mode_diff!=0, width(1) discrete percent xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-40(20)40) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_emp50_mode0.eps", replace
	graph export "$graphs/figA4_diff_mode_emp50_mode0.jpg", replace
	
	count if abs(diff_mode_sal)<=50 & employment_sal>=50 & mode_diff_sal!=0
	histogram bin_sal if abs(diff_mode_sal)<=50 & employment_sal>=50 & mode_diff_sal!=0, width(1) percent discrete xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-40(20)40) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_sal_emp50_mode0.eps", replace
	graph export "$graphs/figA4_diff_mode_sal_emp50_mode0.jpg", replace
	
	*Leave out bunched workers in calculation
	count if abs(diff_mode_leave_out)<=50 & employment_leave_out>=50
	histogram bin_leave if abs(diff_mode_leave_out)<=50 & employment_leave_out>=50, width(1) discrete percent xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-40(20)40) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_emp50_leave_out.eps", replace
	graph export "$graphs/figA4_diff_mode_emp50_leave_out.jpg", replace
	
	count if abs(diff_mode_sal_leave_out)<=50 & employment_sal_leave_out>=50
	histogram bin_sal_leave if abs(diff_mode_sal_leave_out)<=50 & employment_sal_leave_out>=50, width(1) discrete percent xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-40(20)40) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_sal_emp50_leave_out.eps", replace
	graph export "$graphs/figA4_diff_mode_sal_emp50_leave_out.jpg", replace
	
	count if abs(diff_mode_leave_out)<=50 & employment_leave_out>=50 & mode_diff_leave_out!=0
	histogram bin_leave if abs(diff_mode_leave_out)<=50 & employment_leave_out>=50 & mode_diff_leave_out!=0, width(1) discrete percent xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-40(20)40) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_emp50_mode0_leave_out.eps", replace
	graph export "$graphs/figA4_diff_mode_emp50_mode0_leave_out.jpg", replace
	
	count if abs(diff_mode_sal_leave_out)<=50 & employment_sal_leave_out>=50 & mode_diff_sal_leave_out!=0
	histogram diff_mode_sal_leave_out if abs(diff_mode_sal_leave_out)<=50 & employment_sal_leave_out>=50 & mode_diff_sal_leave_out!=0, width(1) percent discrete xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-40(20)40) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_sal_emp50_mode0_leave_out.eps", replace
	graph export "$graphs/figA4_diff_mode_sal_emp50_mode0_leave_out.jpg", replace
	
	*Use aggregate by 0.05% changes in base pay
	count if abs(diff_mode_leave_out)<=50 & employment_leave_out>=50 & mode_diff_leave_out!=0
	histogram diff_mode_leave_out if abs(diff_mode_leave_out)<=5 & employment_leave_out>=50 & mode_diff_leave_out!=0, width(0.05) discrete percent xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-5(1)5) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_emp50_mode0_leave_out_small.eps", replace
	graph export "$graphs/figA4_diff_mode_emp50_mode0_leave_out_small.jpg", replace

	count if abs(diff_mode_sal_leave_out)<=50 & employment_sal_leave_out>=50
	histogram diff_mode_sal_leave_out if abs(diff_mode_sal_leave_out)<=5 & employment_sal_leave_out>=50, width(0.05) percent discrete xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-5(1)5) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_sal_emp50_mode_leave_out_small.eps", replace
	graph export "$graphs/figA4_diff_mode_sal_emp50_mode_leave_out_small.jpg", replace
	
	count if abs(diff_mode_sal_leave_out)<=5 & employment_sal_leave_out>=50 & mode_diff_sal_leave_out!=0
	histogram diff_mode_sal_leave_out if abs(diff_mode_sal_leave_out)<=5 & employment_sal_leave_out>=50 & mode_diff_sal_leave_out!=0, width(0.05) percent discrete xtitle("Change in Base Pay (%), Relative to Modal Change within Firm") ytitle("Percent") xlabel(-5(1)5) ylabel(0(10)50)
	graph export "$graphs/figA4_diff_mode_sal_emp50_mode0_leave_out_small.eps", replace
	graph export "$graphs/figA4_diff_mode_sal_emp50_mode0_leave_out_small.jpg", replace
	

log close 